﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; 
using System.Data;  

namespace Meeting.Public
{
    public class CommonFunction
    {
        #region//获取Request的值 getRequestValue
        /// <summary>
        /// 获取Request值，Request有明的和暗的Request.Form两种，此处首先尝试明的，再尝试暗的，都找不到则返回默认值""。
        /// </summary>
        /// <param name="sRequest">Request的名称</param>
        /// <returns>返回Request值，为Null则返回默认值""</returns>
        public static string getRequestValue(string sRequest)
        {   //获取Request的值
            return getRequestValue(sRequest, "");
        }
        /// <summary>
        /// 获取Request值，Request有明的和暗的Request.Form两种，此处首先尝试明的，再尝试暗的，都找不到则返回默认值""。
        /// </summary>
        /// <param name="sRequest">Request的名称</param>
        /// <param name="sDefault">默认值</param>
        /// <returns>返回Request值，为Null则返回默认值""</returns>
        public static string getRequestValue(string sRequest, string sDefault)
        {   //获取Request的值
            System.Web.HttpRequest Request = HttpContext.Current.Request;
            string result = Request[sRequest];   //明Request
            if (string.IsNullOrEmpty(result)) result = Request.Form[sRequest];    //暗Request
            if (string.IsNullOrEmpty(result)) result = sDefault;
            if (string.IsNullOrEmpty(result)) result = "";
            return result;
        }
        #endregion
        public static string removeAttackOfSql(string sql)
        {
            if (string.IsNullOrEmpty(sql)) return "";
            //关键字
            string[] sKeys = new string[] { "exec", "insert", "delete", "alter", "update", "drop", "script", "java" };
            string s = sql.Replace("'", "''").Replace(",", "，").Replace(";", "；");
            foreach (string siKey in sKeys) rqs(ref s, siKey);
            return s;
        }
         
        private static string rqs(ref string str, string sKeyWord)  //大写时移除
        { if (str.ToUpper().Contains(sKeyWord.ToUpper())) str = str.ToUpper().Replace(sKeyWord.ToUpper(), ""); return str; }


        public static string VarToStr(object obj)
        {
            try
            {
                return obj.ToString();
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 格式化路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string FormatPath(string path)
        {
            if (path.Length < 1) return path;
            if (path.Substring(path.Length - 1) != @"\")
                return path += @"\";
            return path;
        }

        public static string GetDateString(DateTime dt)
        {
            return dt.ToString("yyyyMMdd");
        }

        #region//tryNullTo：强制转换默认值
        /// <summary>
        /// 强制转换默认值
        /// </summary>
        /// <param name="oValue"></param>
        /// <param name="oDefault"></param>
        /// <returns></returns>
        public static object tryNullTo(object oValue, object oDefault)
        {   //强制转换默认值
            try
            { if (oValue == null) return oDefault; else return oValue; }
            catch (Exception x)
            { return oDefault; }
        }
        public static string tryNullToString(object oValue, string oDefault)
        { return tryNullTo(oValue, oDefault).ToString(); }
        #endregion

        #region //varToIntDef：字符转成数字，失败返回默认值
        /// <summary>
        /// 字符转成数字Int整型，默认值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="vDefault">默认值</param>
        /// <returns>转换成功则返回，失败则返回默认值</returns>
        public static int varToIntDef(object str, int vDefault)
        {   //
            int result = vDefault;
            if (str == null) return result;
            try
            { result = Convert.ToInt32(str); }
            catch (Exception x) { x = null; }
            //
            return result;
        }
        /// <summary>
        /// 字符转成数字Float，默认值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="vDefault">默认值</param>
        /// <returns>转换成功则返回，失败则返回默认值</returns>
        public static double varToFloatDef(object str, double vDefault)
        {   //
            double result = vDefault;
            if (str == null) return result;
            try
            { result = Convert.ToDouble(str); }
            catch (Exception x) { }
            //
            return result;
        }
        /// <summary>
        /// 变量转换成string，默认值
        /// </summary>
        /// <param name="str">变量</param>
        /// <param name="vDefault">默认值</param>
        /// <returns>返回结果</returns>
        public static string varToStringDef(object str, string vDefault)
        {   //
            string result = vDefault;
            if (str == null) return result;
            try
            { result = str.ToString(); }
            catch (Exception x) { }
            //
            if (result == null) result = vDefault;
            return result;
        }
        /// <summary>
        /// 变量转换成dateTime，默认值
        /// </summary>
        /// <param name="str">变量</param>
        /// <param name="vDefault">默认值</param>
        /// <returns>返回结果</returns>
        public static DateTime varToDateTimeDef(object str, DateTime vDefault)
        {
            DateTime result = vDefault;
            if (str == null) return result;
            //
            try
            { result = Convert.ToDateTime(str); }
            catch (Exception x) { }
            //
            if (result == null) result = vDefault;
            return result;
        }
        /// <summary>
        /// 值强制转化为逻辑型，如果出错，返回默认值
        /// </summary>
        /// <param name="value">要处理的值</param>
        /// <param name="vDefault">默认值</param>
        /// <returns></returns>
        public static bool varToBoolDef(object value, bool vDefault)
        {
            bool result = vDefault;
            if (value == null) return result;
            //
            try
            { result = Convert.ToBoolean(value); }
            catch (Exception x) { }
            //
            if (result == null) result = vDefault;
            return result;
        }
        #endregion


    }
}
